﻿var RequestInfomationUserCode = "";
Chenxin.UI.RequestManage = Chenxin.extend(Chenxin.UI.PageCommon, {
    autoInitForm: false,
    actionUrl: 'service/confirmRequestChange.ashx',
    createGrid: function() {
        return new Chenxin.UI.Pagination({
            actionUrl: this.actionUrl + '?op=getlist',
            domId: 'page',
            renderTo: 'request_table',
            render: function(data) {
                document.getElementById('request_table').innerHTML = data;
            }
        });
    },
    loadInfo: function(flg) {
        var _this = this;
        Chenxin.ajax({
            type: "get",
            dataType: "text",
            paramType: 'json',
            url: this.actionUrl,
            data: { op: 'getinfo', requestid: this.currentId },
            onSuccess: function(data, msg) {
                if (data.requestinfo) {
                    _this.requestinfo = data.requestinfo;
                    _this.housestatus = data.status;
                    RequestInfomationUserCode = _this.requestinfo.RequestUserCode;
                    if (flg) {
						_this.showChangeConfirm(true);
                        _this.changeConfirmWin.setDisable();
						
                    } else {
						_this.showChangeConfirm();
                        _this.changeConfirmWin.setEnable();
                    }
                }
            }
        });
    },
    loadhouseInfo: function() {
        var _this = this;
        Chenxin.ajax({
            type: "get",
            dataType: "text",
            paramType: 'json',
            url: this.actionUrl,
            data: { op: 'gethouseinfo', requestid: this.currentId },
            onSuccess: function(data, msg) {
                if (data.requestinfo) {
                    _this.houseInfo = data.requestinfo;
                    _this.showCustomerInfo();
                }
            }
        });
    },
    cancle: function() {
    },
    initEvent: function() {
        var _this = this;
        $('.a_auditing').live('click', function() {
            _this.currentId = this.name;
            _this.loadInfo();
        });
        $('.a_auditing_detail').live('click', function() {
            _this.currentId = this.name;
            _this.loadInfo(true);

        });
        $(".a_viewowner").live('click', function() {
            _this.currentId = this.name;
            _this.loadhouseInfo();
        });
        $('.export').live('click', function() {
            _this.exportData();
        });
    },
    createSearch: function() {
        return new Chenxin.UI.ConfirmSearch({
            scope: this
        });
    },
    initComponent: function() {
        Chenxin.UI.RequestManage.superclass.initComponent.call(this);
    },
    /**
    *@description 请求变更/变更确认
    */
    showChangeConfirm: function(flg) {
        var _this = this;
        if (!this.currentId) {
            alert('请先选择一条房源记录！');
            return;
        }
        if (!this.changeConfirmWin) {
            this.changeConfirmWin = new Chenxin.UI.ConfirmationForm({
                currentId: this.currentId,
                scope: this,
                afterGetFormJson: function(json) {
                    delete json.nowStatus;
                    if (this.changeConfirmWin.op == 'confirm') {
                        json.RecordId = this.changeConfirmWin.currentId;
                        json.HouseId = this.requestinfo.HouseId;
                        json.RequestUserCode = json.RequestUserId;
                        delete json.Address;
                        delete json.AddPerson;
                        delete json.AddDepart;
                        delete json.RequestUserId;
                    } else if (this.changeConfirmWin.op == 'refuse') {
                        json.RecordId = this.changeConfirmWin.currentId;
                        json.RequestUserCode = json.RequestUserId;
						json.HouseId =_this.requestinfo.HouseId;
                        delete json.Address;
                        delete json.AddPerson;
                        delete json.AddDepart;
                        delete json.RequestUserId;
                    } else {
                        Chenxin.apply(json, this.currentModel);
                        this.changeConfirmWin.additionalParam = {};
                    }
                    json.Mobile = json.request_customerMoblie;
                    json.Phone = json.request_customerPhone;
                    json.CustomerName = json.request_customerName;
                    delete json.request_customerName
                    delete json.request_customerPhone
                    delete json.request_customerMoblie
                }
            });
        }
		var returnValue = false;
		if(flg) {
			returnValue= this.changeConfirmWin.show("getinfo");
			this.changeConfirmWin.setTitle('请求查看');
		}else {
			returnValue= this.changeConfirmWin.show("confirm");
			this.changeConfirmWin.setTitle('请求确认');
		}
        if (returnValue) {
            var _data = {};
            Chenxin.apply(_data, this.requestinfo);
            _data.Address = _this.requestinfo.HouseAddress;
            _data.AddPerson = _this.requestinfo.oldUserName;
            _data.AddDepart = _this.requestinfo.oldDepart;
            _data.RequestUserId = _this.requestinfo.RequestUserCode;
            var temp = _this.housestatus;
            if (temp == 10)
                temp = '有效';
            else if (temp == 20)
                temp = '暂缓';
            else if (temp == -1)
                temp = '无效';
            else if (temp == 30)
                temp = '封盘';
            else
                temp = '';
            _data.nowStatus = temp;
            _data.request_customerName = _this.requestinfo.CustomerName;
            _data.request_customerPhone = _this.requestinfo.Phone;
            _data.request_customerMoblie = _this.requestinfo.Mobile;
            _data.ResonRemarks = _this.requestinfo.ResonRemarks;
            this.changeConfirmWin.fitData(_data);

            if (_data.ChangeType == '10') {//状态变更
                $("#changeconfirmation_requestChangeStyle").removeAttr("disabled"); //.attr("disabled", false);
                $("#requestChangePersonId").attr("disabled", "disabled");
                $("#requestChangePersonId").val("");
                $("#request_customerName").attr("disabled", "disabled");
                $("#request_customerPhone").attr("disabled", "disabled");
                $("#request_customerMoblie").attr("disabled", "disabled");

                $("#request_tr1").show();
                $("#request_tr2").hide();
                $("#request_tr3").hide();
                $("#request_tr4").hide();
                $("#request_tr5").hide();
                $("#request_tr6").hide();
                $("#request_tr7").hide();
                //_this.contactType.setEnable();
            } else if (_data.ChangeType == '20') {//所属人变更
                $("#changeconfirmation_requestChangeStyle").attr("disabled", true);
                $("#requestChangePersonId").removeAttr("disabled");
                $("#request_customerName").removeAttr("disabled");
                $("#request_customerPhone").removeAttr("disabled");
                $("#request_customerMoblie").removeAttr("disabled");
                $("#request_tr1").hide();
                $("#request_tr2").show();
                $("#request_tr3").show();
                $("#request_tr4").show();
                $("#request_tr5").show();
                $("#request_tr6").show();
                $("#request_tr7").show();
                // _this.contactType.setDisable();
            } else if (_data.ChangeType == '30') {
                $("#changeconfirmation_requestChangeStyle").removeAttr("disabled");
                $("#requestChangePersonId").removeAttr("disabled");
                $("#request_customerName").removeAttr("disabled");
                $("#request_customerPhone").removeAttr("disabled");
                $("#request_customerMoblie").removeAttr("disabled");

                $("#request_tr1").show();
                $("#request_tr2").show();
                $("#request_tr3").show();
                $("#request_tr4").show();
                $("#request_tr5").show();
                $("#request_tr6").show();
                $("#request_tr7").show();
                //_this.contactType.setEnable();
            }
            this.changeConfirmWin.currentId = this.currentId;
        }
    },
    showCustomerInfo: function() {
        var _this = this;
        if (!this.HouseCustomerForm) {
            this.HouseCustomerForm = new Chenxin.UI.HouseCustomerForm({
                op: 'view',
                beforeFitData: function(model) {
                    _this.HouseCustomerForm.currentHouseInfo = _this.houseInfo;
                }
            });
        }
        this.getModel(_this.houseInfo.HouseId, this.HouseCustomerForm.fitData, this.HouseCustomerForm, 'service/CustomerMgr.ashx', 'getmodel', { flg: 'house' });
        this.HouseCustomerForm.show('view', { currentId: _this.currentId });
        this.HouseCustomerForm.setDisable();
        this.HouseCustomerForm.setTitle('查看业主信息');
    }
});


/**
*查询面板
*/
Chenxin.UI.ConfirmSearch = Chenxin.extend(Chenxin.UI.SearchCommon, {
    id: 'request_search',
    initEvent: function() {
        var _this = this;
        $('#btn_search').click(function() {
			_this.scope.reloadGrid();
        });
    },
    createComboList: function() {
        var tracetype = new Chenxin.UI.Combo({
            containerId: '#requeststatus',
            tbxWidth: 40,
            items: comboCommon.requestStatus
        });
        var appflg = new Chenxin.UI.Combo({
            containerId: '#appflg',
            tbxWidth: 40,
            items: comboCommon.Appflg,
			defVal: "0"
        });
        var combl = new Chenxin.UI.StructComboList();
        var structs = combl.comboList;
        structs.push(tracetype);
        structs.push(appflg);
        return structs;
    },
    initComponent: function() {
        Chenxin.UI.ConfirmSearch.superclass.initComponent.call(this);
    }
});

/**
*请求变更/变更确认
*/
Chenxin.UI.ConfirmationForm = Chenxin.extend(Chenxin.UI.FormCommon, {
    actionUrl: 'service/ConfirmRequestChange.ashx',
    id: 'changeConfirmationWin',
    renderTo: 'jqm_changeconfirmation',
	saveSucc:function(){
		_this.scope.grid.reload({},true,true);
	},
    initFormValidator: function() {
        _this = this;
        $("#requestChangePersonId").formValidator({ validatorgroup: this.id, onshow: "请输入工号", onfocus: "输入工号", oncorrect: "输入正确" }).ajaxValidator({
            url: "service/ConfirmRequestChange.ashx",
            datatype: "json",
            data: "houseid=" + _this.currentId + "&op=validate",
            success: function(data) {
                if (data.success == 0) {
                    return false;
                } else {
                    _this.requestUser = data.data.model;

                    if (data.success == 0) {
                        $('#request_name').val('');
                        $('#request_department').val('');
                        return false;
                    } else {
                        $('#request_name').val(data.data.model.UserName);
                        $('#request_department').val(data.data.model.DepartName);
                        return true;
                    }
                }
            },
            buttons: $(".btn_n_2"),
            error: function() { alert("服务器没有返回数据，可能服务器忙，请重试"); },
            onerror: "员工号不存在",
            onwait: "正在校验，请稍候..."
        });
        $("#requestChangeTxtSummary").formValidator({ validatorgroup: this.id,
            onshow: "", onfocus: "", oncorrect: " "
        }).inputValidator({
            min: 1, empty: { emptyerror: "请求说明不能为空！" }, onerror: "请求说明不能为空！"
        });
        $("#ResonRemarks").formValidator({ validatorgroup: this.id,
            onshow: "", onfocus: "", oncorrect: " "
        }).inputValidator({
            min: 1, empty: { emptyerror: "备注不能为空！" }, onerror: "备注不能为空！"
        });
        $("#request_customerName").formValidator({ validatorgroup: this.id,
            onshow: "请输入客户姓名！", onfocus: "请输入客户姓名", oncorrect: "输入正确"
        }).inputValidator({
            min: 1, empty: { emptyerror: "客户姓名不能为空！" }, onerror: "客户姓名不能为空！"
        });
        //联系电话
        $("#request_customerPhone").formValidator({ validatorgroup: this.id,
            onshow: "请输入电话！", onfocus: "请输入电话", oncorrect: " "
        }).functionValidator({
            fun: function(val, elem) {
                if ($("#request_customerPhone").val() == "" && $("#request_customerMoblie").val() == "") {
                    return false;
                } else {
                    return true;
                }
            }, onerror: "请输入手机号码或电话！"
        });
        //手机号码
        $("#request_customerMoblie").formValidator({ validatorgroup: this.id,
            onshow: "请输入手机号！", onfocus: "请输入手机号", oncorrect: " "
        }).functionValidator({
            fun: function(val, elem) { if (val == "") return true; return val.isMobile(); }, onerror: "请输入正确的手机号"
        }).functionValidator({
            fun: function(val, elem) {
                if ($("#request_customerPhone").val() == "" && $("#request_customerMoblie").val() == "") {
                    return false;
                } else {
                    return true;
                }
            }, onerror: "请输入手机号码或电话！"
        });

    },
    createForm: function() {
        return Chenxin.FormCommon.houseConfirmationForm('changeconfirmation');
    },
    createComboList: function() {
        this.contactType = new Chenxin.UI.Combo({
            name: 'HouseStatus',
            containerId: '#changeconfirmation_requestChangeStyle',
            key: 'House.Status',
            items: comboCommon.houseStatus
        });

        return [this.contactType];
    },
    initEvent: function() {
        var _this = this;
        $('#changeconfirmation_Save').live('click', function() {
            _this.op = 'confirm';
            _this.save();
			return false;
        });
        $('#changeconfirmation_Cancle').live('click', function() {
            _this.hide();
        });
        $('#changeconfirmation_Refuse').live('click', function() {
            _this.op = 'refuse';
            _this.save();
        });
        $('#changeConfirmationWin input:radio[name="ChangeType"]').live('click', function() {
            if (this.value == '10') {//状态变更
                $("#changeconfirmation_requestChangeStyle").removeAttr("disabled"); //.attr("disabled", false);
                $("#requestChangePersonId").attr("disabled", "disabled");
                $("#requestChangePersonId").val("");
                $("#request_customerName").val("");
                $("#request_customerPhone").val("");
                $("#request_customerMoblie").val("");
                $("#request_name").val("");
                $("#request_department").val("");
                $("#request_customerName").attr("disabled", "disabled");
                $("#request_customerPhone").attr("disabled", "disabled");
                $("#request_customerMoblie").attr("disabled", "disabled");
                $("#request_tr1").show();
                $("#request_tr2").hide();
                $("#request_tr3").hide();
                $("#request_tr4").hide();
                $("#request_tr5").hide();
                $("#request_tr6").hide();
                $("#request_tr7").hide();
                // _this.contactType.setEnable();
            } else if (this.value == '20') {//所属人变更
                $("#changeconfirmation_requestChangeStyle").attr("disabled", "disabled");
                $("#requestChangePersonId").removeAttr("disabled");
                $("#request_customerName").removeAttr("disabled");
                $("#request_customerPhone").removeAttr("disabled");
                $("#request_customerMoblie").removeAttr("disabled");
                $("#request_tr1").hide();
                $("#request_tr2").show();
                $("#request_tr3").show();
                $("#request_tr4").show();
                $("#request_tr5").show();
                $("#request_tr6").show();
                $("#request_tr7").show();
                // _this.contactType.setDisable();
            } else if (this.value == '30') {
                $("#changeconfirmation_requestChangeStyle").removeAttr("disabled");
                $("#requestChangePersonId").removeAttr("disabled");
                $("#request_customerName").removeAttr("disabled");
                $("#request_customerPhone").removeAttr("disabled");
                $("#request_customerMoblie").removeAttr("disabled");
                $("#request_tr1").show();
                $("#request_tr2").show();
                $("#request_tr3").show();
                $("#request_tr4").show();
                $("#request_tr5").show();
                $("#request_tr6").show();
                $("#request_tr7").show();
                // _this.contactType.setEnable();
            }
        });
    },
    formValidator: function(model) {
        var _this = this;
        var status = true;
        if ((model.RequestUserCode == RequestInfomationUserCode) && (model.ChangeType == '20')) {
            $("#requestChangePersonId").unFormValidator(true);
            $("#request_customerName").unFormValidator(true);
            $("#request_customerPhone").unFormValidator(true);
            $("#request_customerMoblie").unFormValidator(true);
            $("#ResonRemarks").unFormValidator(false);
            status = jQuery.formValidator.pageIsValid(this.id);
        } else if ((model.RequestUserCode == RequestInfomationUserCode) && (model.ChangeType == '30')) {
            $("#requestChangePersonId").unFormValidator(true);
            $("#request_customerName").unFormValidator(true);
            $("#request_customerPhone").unFormValidator(true);
            $("#request_customerMoblie").unFormValidator(true);
            $("#ResonRemarks").unFormValidator(false);
            status = this.checkCombo() && jQuery.formValidator.pageIsValid(this.id);
        } else {
            switch (model.ChangeType) {
                case '10':
                    $("#requestChangePersonId").unFormValidator(true);
                    $("#request_customerName").unFormValidator(true);
                    $("#request_customerPhone").unFormValidator(true);
                    $("#request_customerMoblie").unFormValidator(true);
                    status = this.checkCombo() && jQuery.formValidator.pageIsValid(this.id);
                    break;
                case '20':
                    $("#request_customerName").unFormValidator(false);
                    $("#request_customerPhone").unFormValidator(false);
                    $("#request_customerMoblie").unFormValidator(false);
                    $("#requestChangePersonId").unFormValidator(false);
                    status = jQuery.formValidator.pageIsValid(this.id);
                    break;
                case '30':
                    $("#request_customerName").unFormValidator(false);
                    $("#request_customerPhone").unFormValidator(false);
                    $("#request_customerMoblie").unFormValidator(false);
                    $("#requestChangePersonId").unFormValidator(false);
                    status = jQuery.formValidator.pageIsValid(this.id) && this.checkCombo();
                    break;
                default:
                    status = false;
                    break;

            }
        }
        $("#request_customerName").unFormValidator(false);
        $("#request_customerPhone").unFormValidator(false);
        $("#request_customerMoblie").unFormValidator(false);
        $("#requestChangePersonId").unFormValidator(false);
        $("#ResonRemarks").unFormValidator(false);
        if (!status) {
            alert('请填写完必填(选)项再进行表单提交！');
        }
        return status;

    },
    initComponent: function() {
        Chenxin.UI.ConfirmationForm.superclass.initComponent.call(this);
    }
});
/**
    *@description 变更确认表单
    *@comboIdPrefix DIV下拉框和提交按钮前缀
    */
Chenxin.FormCommon.houseConfirmationForm=function(comboIdPrefix) {
return '<table class="tbl_f jqTabWh"><tbody> <tr><th style="width:80px; height:25px;">房源编号：</th><td style="width:340px;" ><input type="text" name="HouseCode" readonly="readonly" class="inputbox_n"/></td></tr><tr><th style="width:80px; height:25px;">地址：</th><td><input type="text" name="Address" readonly="readonly" class="inputbox_n"/></td></tr><tr><th style="width:80px; height:25px;">原所属人员：</th> <td><input type="text" name="AddPerson" class="inputbox_n" readonly="readonly" /></td></tr><tr><th style="width:80px; height:25px;">原所属门店：</th> <td><input type="text" name="AddDepart" class="inputbox_n" readonly="readonly" /></td></tr><tr><th style="width:80px; height:25px;">目前状态：</th> <td><input type="text" name="nowStatus" class="inputbox_n" readonly="readonly" /></td></tr><tr><th style="width:80px; height:25px;">请求方式：</th><td colspan="3"><input value="10" name="ChangeType" id="requestStyle1" type="radio" />变更状态<input value="20"  name="ChangeType" id="requestStyle2" type="radio"/>变更所属人<input checked="checked" value="30" name="ChangeType" id="requestStyle3" type="radio" />变更状态及所属人</td></tr><tr id ="request_tr1"><th id="request_1" style="width:80px; height:25px;">状态变更：</th><td style="width:250px;"><div style="z-index: 897; margin-right: 5px;" id="' + comboIdPrefix + '_requestChangeStyle"  class="SelectWrapSingle"></div></td></tr><tr id ="request_tr2"><th id="request_2">目标人工号：</th><td ><div style="float:left"><input disabled="disabled" name="RequestUserId" id="requestChangePersonId"  maxlength="20" class="inputbox_n" type="text" /></div><div style="float:left" id="requestChangePersonIdTip"/></td></tr><tr id ="request_tr3"><th  id="request_3" >目标人姓名：</th><td><input name="RequestUser" class="inputbox_n" readonly="readonly" type="text" id ="request_name" /></td></tr><tr id ="request_tr4"><th id="request_4" >目标人门店：</th> <td><input readonly="readonly" class="inputbox_n" type="text" id ="request_department" name="RequestDepart" /></td></tr><tr id ="request_tr5"><th id="request_5"  >客户姓名：</th><td ><div style="float:left"><input disabled="disabled" name="request_customerName" id="request_customerName"  maxlength="20" class="inputbox_n" type="text" /></div><div style="float:left" id="request_customerNameTip"/></td></tr> <tr id="request_tr6"><th id ="request_tr6">客户手机：</th><td ><div style="float:left"><input disabled="disabled" name="request_customerMoblie" id="request_customerMoblie"  maxlength="20" class="inputbox_n" type="text" /></div><div style="float:left" id="request_customerMoblieTip"/></td></tr> <tr id ="request_tr7"><th id="request_7">客户电话：</th><td ><div style="float:left"><input disabled="disabled" name="request_customerPhone" id="request_customerPhone"  maxlength="50" class="inputbox_n" type="text" /></div><div style="float:left" id="request_customerPhoneTip"/></td><th id="request_8" style=\"float:left;\"></th></tr>   <tr><th></th><td><span style=\"color:red; font-weight:bold;\">备注将会显示在跟单信息中，请勿将电话写入备注！</span><td> </tr><tr><th valign="top"  >请求理由：</th><td colspan="3"><textarea name="Remarks" onkeypress="if (this.value.length>=200){event.returnValue=false}"  onchange="if (this.value.length>=200){this.value=this.value.substr(0, 200)}" id ="requestChangeTxtSummary" cols="40" rows="3" class="textarea_n" style="width: 450px; height: 25px; padding:5px;"></textarea></td></tr><tr><th valign="top"  >备注：</th><td colspan="3"><textarea name="ResonRemarks" onkeypress="if (this.value.length>=200){event.returnValue=false}"  onchange="if (this.value.length>=200){this.value=this.value.substr(0, 200)}" id ="ResonRemarks" cols="40" rows="3" class="textarea_n" style="width: 450px; height: 25px; padding:5px;"></textarea></td></tr><tr align="right"><td colspan="4"><div style="float:right"><input type="button"  id="' + comboIdPrefix + '_Save"  class="btn_n_2"  value="通过" />&nbsp;<input type="button"  id="' + comboIdPrefix + '_Refuse" value="拒绝" class="btn_n_2" />&nbsp;<a id="' + comboIdPrefix + '_Cancle" class="btn_n fl"  style="float:right" ><span>关闭</span></a></div></td></tr></tbody> </table>'
};

$(document).ready(function() {
    var requestMgr = new Chenxin.UI.RequestManage();
});





